<?php
namespace app\mobile\controller;
use think\AjaxPage;
use think\Page;
use think\Db;
use phpDocumentor\Reflection\DocBlock\Tag\VarTag;
class Exchange extends MobileBase {
	
    //兑换列表
    public function exchange_list() {
        //查询兑换上架列表
        //查询分类
        $exchange_catrgory = M("exchange_category")->limit(2)->select();
        $exchange_list = M("exchange_goods")->where("is_on_sale = 1")->select();
        $this->assign("exchange_list",$exchange_list);
        $this->assign("exchange_catrgory",$exchange_catrgory);
        return $this->fetch();
    }
    
    //兑换详情
    public function exchange_info() {
        $goods_id = I("id");
        $exchange_info = M("exchange_goods")->where("is_on_sale = 1 and goods_id=$goods_id")->find();
        $goods_images = $exchange_info['goods_images'];
        $goods_images[] = $exchange_info['original_img'];
        $exchange_info['goods_images'] = json_decode($exchange_info['goods_images'],true);
        if(!empty($exchange_info['goods_images'])){
            $goods_images[] = $exchange_info['goods_images'];
        }
        $exchange_info['goods_images'] = $goods_images;
        //$exchange_info['goods_attr'] =  trim($exchange_info['goods_attr']);
        $this->assign("exchange_info",$exchange_info);
        return $this->fetch();
    }
    
    //兑换记录
    public function exchange_record() {
        $user_id = $_SESSION['user']['user_id'];
        if (empty($user_id)) {
            header("location:" . U('Mobile/User/login'));
            exit;
        }
        if(IS_AJAX){
            $id=I("post.order_id");
            $type=I("post.type");
            if($type == 1){
                $data['order_status']=2;//已完成
                exchange_order_action("receive",$id,"客户确认收货");//记录订单操作日志
            }elseif($type == 2){
                $data['is_del'] = 1;//删除记录
            }
            $result = M("exchange_record")->where("id",$id)->save($data);
            $back['status'] = 1;
            $back['msg'] = "操作成功";
            $back['data'] =$result ;
            echo json_encode($back);
            exit();
        }
        $exchange_record = M("exchange_record")
                            ->alias("er")
                            ->join("exchange_goods eg","er.goods_id = eg.goods_id")
                            ->where("er.user_id = $user_id and is_del =0")
                            ->field("er.*,eg.original_img,eg.goods_name,eg.exchange_integral")
                            ->order("er.addtime desc")
                            ->select();
        $this->assign("exchange_record",$exchange_record);
        return $this->fetch();
    }
    
    //兑换提交订单
    public function exchange_orderinfo() {
        $user_id = $_SESSION['user']['user_id'];
        if (empty($user_id)) {
            header("location:" . U('Mobile/User/login'));
            exit;
        }
        $goods_id = I("id");
        $exchange_info = M("exchange_goods")->where("is_on_sale = 1 and goods_id=$goods_id")->find();
        $address = M("user_address")->where("user_id =$user_id and is_default=1 ")->find();
        $this->assign("address",$address);
        $this->assign("exchange_info",$exchange_info);
        return $this->fetch();
    }
    
    public function create_order(){
        if(IS_AJAX){
            $user_id = $_SESSION['user']['user_id'];
            if (empty($user_id)) {
                $back['status'] = 0;
                $back['msg'] = "兑换失败,请先登陆";
                $back['data'] = "用户信息获取失败";
                echo json_encode($back);
                exit();
            }
            
            //获取用户信息
            $user_info =M("users")->where("user_id",$user_id)->find();
            if($user_info['is_lock']){
                $back['status'] = 0;
                $back['msg'] = "兑换失败,您的账户被冻结";
                $back['data'] = "账户被冻结";
                echo json_encode($back);
                exit();
            }
            $goods_id = I("post.goods_id");//兑换的商品id
            $address_id = I("post.address_id");
            $shipping_name =I("post.shipping_name");
            if($shipping_name == "货到付款"){
                $shipping_code = "huodaofukuan";
            }else{
                $shipping_code = "fengtan";
            }
            $num = I("post.num");//兑换的数量
            //获取商品信息
            $goods_info = M("exchange_goods")->where("goods_id",$goods_id)->find();
            if($goods_info['store_count'] <$num){
                $back['status'] = 0;
                $back['msg'] = "兑换失败,库存不足";
                $back['data'] = "库存不足";
                echo json_encode($back);
                exit();
            }
            $need_pay = $goods_info['exchange_integral']*$num;
            if($goods_info['is_free_shipping'] == 0){//是否包邮
                if($shipping_name!="货到付款"){
                    $need_pay += 1200;//抵扣快递费
                }
            }
            //判断积分是否足以支付
            if($user_info['pay_points'] > $need_pay){
                //生成兑换记录
                $data['goods_id'] = $goods_id;
                $data['user_id'] = $user_id;
                $data['num'] = $num;
                $data['address_id'] = $address_id;
                $data['pay_stattus'] = 1;
                $data['shipping_name'] = $shipping_name;
                $data['shipping_code'] = $shipping_code;
                $data['order_status'] = 0;
                $data['shipping_status'] = 0;
                $data['integral_money'] = $goods_info['exchange_integral']*$num;
                $data['total_amount'] = $need_pay;
                $data['addtime'] = time();
                $record_result = M("exchange_record")->add($data);
                if($record_result){
                    exchange_order_action("insert",$record_result,"兑换订单");//记录订单操作日志
                }
                //扣除兑换需要银酒币
                $data1['pay_points'] = $user_info['pay_points'] - $need_pay;
                $user_result = M("users")->where("user_id",$user_id)->save($data1);//扣除
                income_action(0,5,"银酒币兑换商品",-$need_pay,$user_id);
                
                //商品信息修改
                $data2['sales_sum'] = $goods_info['sales_sum']+$num;//加销量
                $data2['store_count'] = $goods_info['store_count']-$num;//减库存
                M("exchange_goods")->where("goods_id",$goods_id)->save($data2);
                if($user_result && $record_result){
                    $back['status'] = 1;
                    $back['msg'] = "兑换成功";
                    $back['data'] = "兑换成功";
                }else{
                    $back['status'] = 0;
                    $back['msg'] = "兑换失败,请稍后重试";
                    $back['data'] = "数据处理错误";
                }
                echo json_encode($back);
                exit();
            }else{
                $back['status'] = 0;
                $back['msg'] = "兑换失败,银酒币不足";
                $back['data'] = "银酒币不足";
                echo json_encode($back);
                exit();
            }
        }
        echo "非法请求";
        exit();
    }
}